package ${controllerPath};

import com.lenovo.cnbu.common.core.domain.R;
import com.lenovo.cnbu.common.core.utils.JacksonUtils;
import com.lenovo.cnbu.common.core.web.controller.BaseTest;
import ${dtoPath}.*;
import ${mapperPath}.${EntityName}Mapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.Date;
import javax.annotation.Resource;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@Slf4j
@AutoConfigureMockMvc
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ${EntityName}ControllerTest extends BaseTest {
    @Resource
    private MockMvc mockMvc;
    @Resource
    private ${EntityName}Mapper ${EntityName}Mapper;
    private static final Long id = 1L;
<#if add>
    /**
    * 新增${table_annotation}
    * @throws Exception 异常
    */
    @Test
    public void insert() throws Exception {
        ${EntityName}AddDto dto = ${EntityName}AddDto.builder()
        <#list columnListNotIdAndTimeAndUser as model>
            <#if model.javaType=="Date">
            .${model.changeColumnName}(new Date())
            </#if>
            <#if model.javaType=="String">
            .${model.changeColumnName}("【单元测试】")
            </#if>
            <#if model.javaType=="Integer">
            .${model.changeColumnName}(1)
            </#if>
            <#if model.javaType=="Long">
            .${model.changeColumnName}(1L)
            </#if>
        </#list>
            .build();
         MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/${lowName}")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JacksonUtils.toString(dto))
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
        long id = Long.parseLong(r.getData() + "");
        ${EntityName}Mapper.deleteById(id);
    }
</#if>
<#if update>
    /**
    * 修改${table_annotation}
    * @throws Exception 异常
    */
    @Test
    public void update() throws Exception {
        ${EntityName}UpdateDto dto = ${EntityName}UpdateDto.builder()
            .id(id)
        <#list columnListNotIdAndTimeAndUser as model>
        <#if model.javaType=="Date">
            .${model.changeColumnName}(new Date())
        </#if>
        <#if model.javaType=="String">
            .${model.changeColumnName}("【单元测试】")
        </#if>
        <#if model.javaType=="Integer">
            .${model.changeColumnName}(1)
        </#if>
        <#if model.javaType=="Long">
            .${model.changeColumnName}(1L)
        </#if>
        </#list>
            .build();
         MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.put("/${lowName}")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JacksonUtils.toString(dto))
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
    }
</#if>
<#if delete>
    /**
    * 删除${table_annotation}
    * @throws Exception 异常
    */
    @Test
    public void deleteBatch() throws Exception {
        List<Long> ids = new ArrayList<>();
        ids.add(2L);
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.delete("/${lowName}")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .queryParam("ids",ids.stream().map(Object::toString).toArray(String[]::new))
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
    }
</#if>
<#if getAll>
    /**
    * 查询所有${table_annotation}
    * @throws Exception 异常
    */
    @Test
    public void getAll() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/${lowName}/getAll")
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
    }
</#if>
<#if getVoById>
    /**
    * 根据id查询${table_annotation}
    * @throws Exception 异常
    */
    @Test
    public void getById() throws Exception {
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/${lowName}/getById/"+id)
            .contentType(MediaType.APPLICATION_FORM_URLENCODED)
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
    }
 </#if>
 <#if page>
    /**
    * 分页查询${table_annotation}
    *
    * @throws Exception 异常
    */
    @Test
    public void page() throws Exception {
        ${EntityName}PageDto dto = ${EntityName}PageDto.builder().build();
        MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/${lowName}/page")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JacksonUtils.toString(dto))
            .header("Authorization", token))
            .andDo(print())
            .andReturn();
        String string = mvcResult.getResponse().getContentAsString(Charset.defaultCharset());
        R r = JacksonUtils.toBean(string, R.class);
        Assert.assertEquals(r.getMsg(), 200, r.getCode());
    }
</#if>
}